package com.bocai.kelalacar.ui.driver.main.act;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CheckLineBean;
import com.bocai.kelalacar.bean.MyAddressBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.main.act.PublishInformationAddressActivity;
import com.bocai.kelalacar.ui.car.main.act.PublishInformationAddressSecondAct;
import com.bocai.kelalacar.ui.driver.main.contract.DriverPublishContract;
import com.bocai.kelalacar.ui.driver.main.module.DriverPublishModule;
import com.bocai.kelalacar.ui.driver.main.presenter.DriverPublishPresenter;
import com.bocai.kelalacar.util.DBManager;
import com.bocai.kelalacar.widget.DetailAddressDialog;
import com.bocai.kelalacar.widget.SorryDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/28.
 */

public class DriverPublishInforAddressActivity extends BaseActivity implements DriverPublishContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_start_address)
    TextView tvStartAddress;
    @BindView(R.id.ll_start_address)
    LinearLayout llStartAddress;
    @BindView(R.id.btn_next)
    Button btnNext;
    @Inject
    DriverPublishPresenter presenter;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv1_change)
    TextView tv1Change;
    @BindView(R.id.img1)
    ImageView img1;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    List<MyAddressBean> list = new ArrayList<>();
    public List<MyAddressBean> options1Items = new ArrayList<>();
    public List<List<MyAddressBean>> options2Items = new ArrayList<>();
    public List<MyAddressBean> options3Items = new ArrayList<>();
    private OptionsPickerView<MyAddressBean> mPvOptions;
    private String startCityId,startDetailAddress;
    private DetailAddressDialog detailAddressDialog;

    @Override
    public int getLayoutId() {
        return R.layout.act_driver_publish_infor_address;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new DriverPublishModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("发布行程");
        initBack(imgBack,llBack);
        presenter.originAreas();
        detailAddressDialog = new DetailAddressDialog(this);
    }

    @OnClick({R.id.btn_next, R.id.ll_start_address,R.id.ll_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next:
                if (startCityId == null||startCityId.equals("")){
                    showErrorTip("请选择始发地");
                }else if (startDetailAddress == null||startDetailAddress.equals("")){
                    showErrorTip("请选择详细地址");
                }else {
                    Intent intent = new Intent(DriverPublishInforAddressActivity.this,DriverPublishInforAddressSecondActivity.class);
                    intent.putExtra("originCityId",startCityId);
                    intent.putExtra("originAddress",startDetailAddress);
                    startActivity(intent);
                }
                break;
            case R.id.ll_start_address:
                if (mPvOptions != null) {
                    mPvOptions.show();
                } else {
                    showErrorTip("正在加载地址选项");
                }
                break;
            case R.id.ll_address:
                if (options3Items != null&&options3Items.size() != 0) {
                    detailAddressDialog.show();
                }else {
                    showErrorTip("请先选择始发地");
                }
                break;
        }
    }

    public void setAddressSelect() {

        if (options1Items.size() <= 0 || options2Items.size() <= 0) {
            options1Items.clear();
            options2Items.clear();

            options1Items = findProvince();
            for (MyAddressBean entity : options1Items) {
                List<List<MyAddressBean>> list2 = new ArrayList<>();
                List<MyAddressBean> list = findCityOrArea(entity);
                if (list.size() > 0) {
                    options2Items.add(list);
                } else {
                    list.add(new MyAddressBean("", "", ""));
                    options2Items.add(list);
                }
            }
        }

    }

    private ArrayList<MyAddressBean> findProvince() {
        ArrayList<MyAddressBean> provice = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            if (list.get(i).getParentId().equals("0")) {
                provice.add(list.get(i));
            }
        }
        return provice;
    }


    public OptionsPickerView initPickView() {
        //选项选择器
        OptionsPickerView<MyAddressBean> pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                startCityId = options2Items.get(options1).get(options2).getId();
                tvStartAddress.setText(options1Items.get(options1).getFullName()+" "+options2Items.get(options1).get(options2).getFullName());
                tvStart.setText("始发地");
                tvStartAddress.setVisibility(View.VISIBLE);
                tv1Change.setVisibility(View.VISIBLE);
                img1.setVisibility(View.VISIBLE);
                options3Items = findCityOrArea(options2Items.get(options1).get(options2));
                tvAddress.setText("");
                startDetailAddress = "";
                btnNext.setEnabled(false);
                btnNext.setTextColor(getResources().getColor(R.color.text_color));
                detailAddressDialog.builder().setList(options3Items, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        tvAddress.setText(options3Items.get(position).getFullName());
                        detailAddressDialog.dismiss();
                        startDetailAddress = options3Items.get(position).getFullName();
                        btnNext.setEnabled(true);
                        btnNext.setTextColor(getResources().getColor(R.color.color_white));
                    }
                });
            }
        }).build();
        //三级联动效果
        pvOptions.setPicker(options1Items, options2Items);
        //设置选择的三级单位
        pvOptions.setSelectOptions(0, 0);

        return pvOptions;

    }

    private ArrayList<MyAddressBean> findCityOrArea(MyAddressBean bean) {
        ArrayList<MyAddressBean> citys = new ArrayList<>();
        for (int i = 0;i<list.size();i++){
            if (list.get(i).getParentId().equals(bean.getId())) {
                citys.add(list.get(i));
            }
        }

        return citys;
    }




    @Override
    public void checkLineSuccess(CheckLineBean bean) {
        if (bean.getPriceList()!=null&&bean.getPriceList().size()!=0) {
            Intent intent = new Intent(DriverPublishInforAddressActivity.this, DriverPublishInforActivity.class);
            intent.putExtra("data", bean);
            startActivityForResult(intent,11);
        }else {
            showErrorTip("请联系后台添加相应的车位数");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 11&&requestCode == 11){
            finish();
        }
    }

    @Override
    public void publishStrokeSuccess() {

    }

    @Override
    public void originAreasList(List<MyAddressBean> list) {
        this.list = list;
        setAddressSelect();
        mPvOptions = initPickView();
    }

    @Override
    public void arriveAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }else if (msg.equals("1")){
            setDialog();
        }else {
            showShortToast(msg);
        }
    }

    public void setDialog(){
        new SorryDialog(this).builder().show();
    }
}
